package com.mall.advertising.service.impl;

import java.util.List;

import com.mall.advertising.domain.TAdvertising;
import com.mall.advertising.domain.TCrow;
import com.mall.advertising.domain.dto.TAdvertisingDto;
import com.mall.advertising.mapper.AdvertisingCrowMapper;
import com.mall.advertising.mapper.TAdvertisingMapper;
import com.mall.advertising.mapper.TCrowMapper;
import com.mall.advertising.service.ITAdvertisingService;
import com.mall.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 广告列表Service业务层处理
 * @date 2025-02-14
 */
@Service
public class TAdvertisingServiceImpl implements ITAdvertisingService
{
    @Autowired
    private TAdvertisingMapper tAdvertisingMapper;

    @Autowired
    private AdvertisingCrowMapper advertisingCrowMapper;

    @Autowired
    private TCrowMapper tCrowMapper;


    /**
     * 查询广告列表
     *
     * @param advertisingId 广告列表主键
     * @return 广告列表
     */
    @Override
    public TAdvertising selectTAdvertisingByAdvertisingId(Long advertisingId)
    {
        return tAdvertisingMapper.selectTAdvertisingByAdvertisingId(advertisingId);
    }

    /**
     * 查询广告列表列表
     *
     * @param tAdvertising 广告列表
     * @return 广告列表
     */
    @Override
    public List<TAdvertising> selectTAdvertisingList(TAdvertising tAdvertising)
    {
        return tAdvertisingMapper.selectTAdvertisingList(tAdvertising);
    }



    /**
     * 新增广告列表
     *
     * @return 结果
     */
    @Transactional
    @Override
    public void insertTAdvertising(TAdvertisingDto tAdvertisingDto) {
        tAdvertisingMapper.insertTAdvertising(tAdvertisingDto);
        Integer advertisingId = tAdvertisingDto.getAdvertisingId();
        List<Integer> crowdId = tAdvertisingDto.getCrowdId();

        Integer pushTarget = tAdvertisingDto.getPushTarget();

        if(pushTarget==1){
            List<TCrow> tCrows = tCrowMapper.selectCrowsAll();
            tCrows.forEach(tCrow -> {
                crowdId.add(tCrow.getId());
            });
        }
        advertisingCrowMapper.insertAdvertisingCrow(advertisingId,crowdId);

    }


    /**
     * 修改广告列表
     *
     * @param
     * @return 结果
     */
    @Override
    public void updateTAdvertising(TAdvertisingDto tAdvertisingDto)
    {
         tAdvertisingMapper.updateTAdvertising(tAdvertisingDto);

        //删除中间表

        Integer advertisingId = tAdvertisingDto.getAdvertisingId();
        List<Integer> crowdId = tAdvertisingDto.getCrowdId();

        Integer pushTarget = tAdvertisingDto.getPushTarget();

        if(pushTarget==1){
            List<TCrow> tCrows = tCrowMapper.selectCrowsAll();
            tCrows.forEach(tCrow -> {
                crowdId.add(tCrow.getId());
            });
        }
        advertisingCrowMapper.insertAdvertisingCrow(advertisingId,crowdId);
    }

    /**
     * 批量删除广告列表
     *
     * @param advertisingIds 需要删除的广告列表主键
     * @return 结果
     */
    @Override
    public int deleteTAdvertisingByAdvertisingIds(Long[] advertisingIds)
    {
        return tAdvertisingMapper.deleteTAdvertisingByAdvertisingIds(advertisingIds);
    }

    /**
     * 删除广告列表信息
     *
     * @param advertisingId 广告列表主键
     * @return 结果
     */
    @Override
    public int deleteTAdvertisingByAdvertisingId(Long advertisingId)
    {
        return tAdvertisingMapper.deleteTAdvertisingByAdvertisingId(advertisingId);
    }

    @Override
    public void updateStatue(Integer advertisingId) {
        TAdvertising tAdvertising = tAdvertisingMapper.selectTAdvertisingByAdvertisingId(Long.valueOf(advertisingId));
        if(tAdvertising.getStatue()==1){
            //下线
          tAdvertisingMapper.updateStatue(advertisingId,2);
         }else{
            //上线
            tAdvertisingMapper.updateStatue(advertisingId,1);
        }


    }
}
